home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14550 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  136 lines

  1. Path: newsstand.cit.cornell.edu!usenet
  2. From: Christopher Kline <ckline@tc.cornell.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Problem with the relationship between friend and derived classes
  5. Date: Sun, 31 Mar 1996 16:19:04 -0500
  6. Organization: Cornell University
  7. Sender: cjk2@cornell.edu (Verified)
  8. Message-ID: <315EF6C8.41C6@tc.cornell.edu>
  9. NNTP-Posting-Host: crystal.tc.cornell.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; IRIX 5.3 IP22)
  14.  
  15. I have a problem involving the relationship between friend classes and
  16. derived classes. 
  17.  
  18. My group is currently writing a simulation framework involving moving
  19. vehicles, each of which have an associated set of dynamics which
  20. control their movement and their interaction with other vehicles in
  21. the simulation. 
  22.  
  23. The problem I am having can be seen in this minimal example (the
  24. errors are included as comments at point where they occur):
  25.  
  26.  
  27. class engine { 
  28.  
  29. public:
  30.   engine(car *c) {
  31.     attachedCar = c;
  32.   };
  33.   
  34.   virtual void update(void) = 0;  
  35.  
  36. protected:
  37.   car *attachedCar;  
  38. };
  39.  
  40. //--------------------
  41.  
  42. class car {
  43.  
  44.   friend engine;
  45.   
  46. public:
  47.   car(void) {
  48.     velocity = 0;
  49.   }
  50.  
  51.   virtual void move(void) = 0;
  52.  
  53. protected:
  54.   engine *e;
  55.   double velocity;
  56.   
  57. };
  58.  
  59. // -------------------
  60.  
  61. class honda_engine : public engine {
  62.  
  63. public:
  64.   honda_engine(honda *h)
  65.     : engine(h) { }
  66.   
  67.   void update(void) {
  68.  
  69.     // Error #1: "engine::attachedCar" is inaccessible   
  70.     // Error #2: "car::velocity" is inaccessible
  71.     attachedCar->velocity++; 
  72.                  
  73.     // Error #3: "honda_miles" is undefined
  74.     honda_miles++;         
  75.   }
  76.  
  77. };
  78.  
  79. //--------------------
  80.  
  81. class honda : public car {
  82.  
  83.   friend honda_engine;
  84.  
  85. public:
  86.   honda(void) {
  87.     honda_miles = 0;
  88.     e = new honda_engine(this);
  89.   }
  90.  
  91.   void move(void) {
  92.     e->update();
  93.   }
  94.  
  95. private:
  96.   int honda_miles;
  97.   
  98. };
  99.  
  100. //--------------------
  101.  
  102.  
  103. Engines are friends of their respective cars and the base classes for
  104. engines and cars are both abstract classes (hence only instances of
  105. the derived classes will be created). 
  106.  
  107. The problem is that I would like:
  108.  
  109. 1) a derived engine to have access to the protected members of the
  110. derived car class of which it is a friend, and of the parent class(es)
  111. of that car.
  112.  
  113. 2) a derived engine to access the correct class of car when it
  114. dereferences the attachedCar member (attachedCar will always be
  115. something derived from car, even if the compiler can't understand
  116. that).
  117.  
  118.  
  119. I understand WHY these three errors are occurring, but I can't figure
  120. out HOW to get my code to behave the way I want it to (i.e., using the
  121. relationship between cars/engines that I've set up).
  122.  
  123. Does anyone have any clue on how to get this to work? It seems like it
  124. would be a fairly common design problem, but I haven't found reference
  125. to it in any of my books or the FAQ.
  126.  
  127. Any help would be greatly appreciated. Also, if you would kindly CC:
  128. me on the reply, that would be great, in case I miss the post in the
  129. newsgroup.
  130.  
  131. -- 
  132. Christopher Kline 
  133. http://www.tc.cornell.edu/~ckline/
  134.  
  135. to compose, Decompose, I wander and slip
  136.